/*
 * Created by JFormDesigner on Fri Dec 06 10:42:34 CST 2024
 */

package com.hafu.view.ShangPin;

import com.hafu.model.SpInfo;
import com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * @author aa
 */
public class ManageShangPinFrm extends JFrame {
    private SpInfoService spInfoService = new SpInfoService();
    public ManageShangPinFrm() {

        initComponents();
        fillTable();
    }

    private void fillTable() {
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        dtn.setRowCount(0);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfos) {
            Object[] rowDate= {spInfo.getId(),spInfo.getName(),spInfo.getPrice(),spInfo.getSellPrice()};
            dtn.addRow(rowDate);
        }
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 查询商品
        String name = textField2.getText();
        SpInfo spInfo = new SpInfo(name);
        ArrayList<SpInfo> spInfos = spInfoService.getSpInfoByName(spInfo);
        DefaultTableModel dtn = (DefaultTableModel) table1.getModel();
        dtn.setRowCount(0);
        if (spInfos.size()>0){
            for (SpInfo spInfo1 : spInfos) {
                Object[] rowDate= {spInfo1.getId(),spInfo1.getName(),spInfo1.getPrice(),spInfo1.getSellPrice()};
                dtn.addRow(rowDate);
            }
        }else{
            JOptionPane.showMessageDialog(null,"没有找到该商品");
        }
    }

    private void table1MouseClicked(MouseEvent e) {
        // TODO add your code here 获取选择行数据
        int row = table1.getSelectedRow();
        //获取行的数据
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row,0);
        String name = (String)dtm.getValueAt(row,1);
        BigDecimal price = (BigDecimal)dtm.getValueAt(row,2);
        BigDecimal sellPrice = (BigDecimal)dtm.getValueAt(row,3);
        //
        textField6.setText(name);
        textField7.setText(String.valueOf(price));
        textField8.setText(String.valueOf(sellPrice));
    }

    private void button2(ActionEvent e) {
        // TODO add your code here 修改事件
        String name = textField6.getText();
        String price = textField7.getText();
        String sellPrice = textField8.getText();
        if (StringUtil.isEmpty(name)){
            JOptionPane.showMessageDialog(null,"商品名字不能为空");
            return;
        }
        if (StringUtil.isEmpty(price)){
            JOptionPane.showMessageDialog(null,"商品进价不能为空");
            return;
        }
        if (StringUtil.isEmpty(sellPrice)){
            JOptionPane.showMessageDialog(null,"商品售价不能为空");
            return;
        }
        BigDecimal price1 = new BigDecimal(price);
        BigDecimal sellPrice1 = new BigDecimal(sellPrice);
        int row = table1.getSelectedRow();
        //获取行的数据
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int chooseId = (int) dtm.getValueAt(row,0);
        SpInfo spInfo = new SpInfo(chooseId,name,price1,sellPrice1);
        Boolean aBoolean = spInfoService.updateSpInfo(spInfo);
        if(aBoolean){
            JOptionPane.showMessageDialog(null,"修改成功");
            fillTable();
        }else{
            JOptionPane.showMessageDialog(null,"修改失败");
        }
    }

    private void button3(ActionEvent e) {
        // TODO add your code here 删除事件
        int row = table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel) table1.getModel();
        int id = (int) dtm.getValueAt(row,0);
        Boolean isDeleteSpInfo = spInfoService.deleteSpInfo(id);
        if(isDeleteSpInfo){
            JOptionPane.showMessageDialog(null,"删除成功");
            fillTable();
        }else{
            JOptionPane.showMessageDialog(null,"删除失败");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        textField1 = new JTextField();
        textField2 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        textField3 = new JTextField();
        textField4 = new JTextField();
        textField5 = new JTextField();
        textField6 = new JTextField();
        textField7 = new JTextField();
        textField8 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u5546\u54c1");
        Container contentPane = getContentPane();

        //---- textField1 ----
        textField1.setText("\u5546\u54c1\u540d\u79f0");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> {
			button1(e);
			button1(e);
		});

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, null},
                    {null, null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u8fdb\u4ef7", "\u552e\u4ef7"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- textField3 ----
        textField3.setText("\u5546\u54c1\u540d\u5b57");

        //---- textField4 ----
        textField4.setText("\u5546\u54c1\u8fdb\u4ef7");

        //---- textField5 ----
        textField5.setText("\u5546\u54c1\u552e\u4ef7");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> {
			button2(e);
			button2(e);
		});

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(17, 17, 17)
                            .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(76, 76, 76)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button1))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addContainerGap(125, Short.MAX_VALUE)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                                    .addGroup(contentPaneLayout.createParallelGroup()
                                        .addComponent(textField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                    .addGap(42, 42, 42)
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(textField7, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                        .addComponent(textField6, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE)
                                        .addComponent(textField8, GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 51, Short.MAX_VALUE)
                                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(button3)
                                        .addComponent(button2))
                                    .addGap(133, 133, 133)))))
                    .addGap(62, 62, 62))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(28, 28, 28)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button1)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE)
                    .addGap(32, 32, 32)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(button2)
                        .addComponent(textField6, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField7, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(textField5, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(button3)
                            .addComponent(textField8, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap(112, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JTextField textField7;
    private JTextField textField8;
    private JButton button2;
    private JButton button3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
